package com.nondo.dean.platform.controller.api.app;

import com.nondo.dean.auth.common.TokenPrincipal;
import com.nondo.dean.auth.common.TokenPrincipalProvider;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Feedback;
import com.nondo.dean.platform.domain.User;
import com.nondo.dean.platform.model.result.FeedBackResult;
import com.nondo.dean.platform.service.FeedbackService;
import com.nondo.dean.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/4/25
 */
@RestController("apiAppFeedback")
@RequestMapping("/{v}/app/feedback")
public class FeedbackController {
    @Autowired
    FeedbackService feedbackService;
    @Autowired
    UserService userService;
    @Autowired
    private TokenPrincipalProvider tokenPrincipalProvider;

    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Feedback> add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Feedback feedback, BindingResult result) {
        View<Feedback> view = new View<Feedback>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {

            TokenPrincipal principal = tokenPrincipalProvider.getTokenPrincipal();
            User user = userService.selectUserByName(principal.getUsername());
            if (user == null) {
                return View.ofError("只有登录后才能进行反馈");
            }

            feedback.setUserId(user.getId());
            feedback.setStoreId(user.getStoreId());
            if (user.getStoreId() == null) {
                return View.ofError("只有拥有店铺才能进行反馈");
            }
            this.feedbackService.add(feedback);
        }
        return view;
    }

    /**
     * 获取意见反馈查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<FeedBackResult>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Feedback> query) {
        View<Page<FeedBackResult>> view = new View<Page<FeedBackResult>>(20000, "获取列表成功");

        Feedback feedback = query.getData();

        Page<FeedBackResult> page = new Page<FeedBackResult>(query.getPage(), query.getLimit(), feedbackService.count(feedback));

        List<FeedBackResult> feedBackResultList = feedbackService.getList(query.getOffset(), query.getLimit(), feedback);

        page.setContent(feedBackResultList);

        view.setData(page);

        return view;
    }

}
